<?php
	/*
 	* @version $Id: checksefurls.php,v 1.1 2010/11/07 12:12:01 marco_manti
 	* @package http://www.gisa.by
 	* @copyright (C) 2006 - 2010 marco_manti(R)
 	* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 	* @description Script check SEF urls, regenerate if not present or delete non existant
 	*
 	*/

        require_once(dirname(dirname(__FILE__)).'/includes/library.php');

        // init global classes
        $dbo = new DB();
        $helper = new HELPER();
        $config = new CONFIG();

        // globals
        $endline = chr(13).chr(10);
        $counter = 0;
        $today = date('Y-m-d H:i');
        $title = ' GENERATE SEF LINKS: ';
        $email_title = 'GISA.by ::' . $title;
        $email = $config->server_root;
        $redirections = array();
        $logmsg = $mysqlerrlog = '';

        // mail headers
        $headers  = "MIME-Version: 1.0\r\n";
        $headers .= "Content-type: text/html; charset=windows-1251\r\n";
        $headers .= "From: GISA.by <gisaby@gmail.com>\r\n";
        $headers .= "X-Mailer: PHP/" . phpversion();
        
        // connect to DB
        if (!$dbo->connect()) {
            $logmsg .= $today.$title.'ERROR - connect to database ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - connect to database ['.$dbo->error().'];', $headers);
        }

        // get array of sef links
        $query = "SELECT newurl, oldurl
                  FROM rlt_redirection
                  WHERE oldurl LIKE '%database%'
                  OR newurl LIKE '%com_agency%' OR newurl LIKE '%com_comprofiler%' OR newurl LIKE '%showEO%'OR newurl LIKE '%com_login%'
                  ORDER BY cpt DESC
                  LIMIT 0, 5000 ";
        
        $objects = $dbo->getObjectsArray($query);
        if (!$objects) {
            $logmsg .= $today.$title.'ERROR - getting fields from _redirections ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - getting fields from _redirections ['.$dbo->error().'];', $headers);
        } else {
            foreach($objects as $item) {
                $redirections[$item->newurl] = $item->oldurl;
            }
        }

        // database links
        $query = "SELECT * FROM rlt_estateagent";
        $objects = $dbo->getObjectsArray($query);
        if (!$objects) {
            $logmsg .= $today.$title.'ERROR - getting fields from _estateagent ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - getting fields from _estateagent ['.$dbo->error().'];', $headers);
        }

        // generating SQL query for new object urls
        $query = "UPDATE rlt_redirection SET cpt = cpt + 10 WHERE newurl LIKE '%showEO%'";
        $dbo->query($query);
        if (!$rows) {
            $logmsg .= $today.$title.'ERROR - increment showEO SEF rate ['.$dbo->error().'];'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - increment showEO SEF rate ['.$dbo->error().'];', $headers);
        }

        $query = "INSERT INTO rlt_redirection (cpt, oldurl, newurl) VALUES ";
        if (count($objects) > 0 ) {
            foreach($objects as $item) {
               $url = 'index.php?option=com_estateagent&act=object&task=showEO&Itemid=14&id='.$item->id;
               $sefurl = 'database/'.$helper->getEASefTitle($item).'.html';

               // generate SEF url if not exists
               if (!array_key_exists($url, $redirections)) {
                   $query .= " (5, '".$sefurl."', '".$url."'), ";
                   $counter++;
               }
            }
            // add new SEF urls to DB
            if ($counter > 0) {
                $query = substr($query, 0, strlen($query) - 2);
                $rows = $dbo->query($query);
                if (!$rows) {
                    $logmsg .= $today.$title.'ERROR - add new SEF urls to _redirection ['.$dbo->error().'];'.$endline;
                    $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
                    mail($email, $email_title, 'ERROR - add new SEF urls to _redirection ['.$dbo->error().'];', $headers);
                }
            }
        } else {
            $logmsg .= $today.$title.'ERROR - _estateagent return empty result;'.$endline;
            $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
            mail($email, $email_title, 'ERROR - _estateagent return empty result;', $headers);
        }

        if ($logmsg == '' && $counter > 0) {
            $logmsg .= $today.$title.'SUCCESS - '.$rows.' links generated;'.$endline;
        } else {
            if ($logmsg == '') $logmsg .= $today.$title.'SUCCESS - nothing to do;'.$endline;
        }

        // perform some database cleanup
        $title = ' DATABASE CLEANUP: ';
        $query = "DELETE FROM rlt_redirection WHERE LENGTH(newurl) = 0 AND ((oldurl NOT LIKE '%.png%') OR (oldurl NOT LIKE '%.jpg%') OR (oldurl NOT LIKE '%.gif%'))";
        $rows = $dbo->query($query);

        if (!$rows) {
            if ($dbo->getLastError()) {
                $logmsg .= $today.$title.'ERROR - add new SEF urls to _redirection ['.$dbo->error().'];'.$endline;
                $mysqlerrlog .= $today.$title.' '.$dbo->error().$endline;
                mail($email, $email_title, 'ERROR - add new SEF urls to _redirection ['.$dbo->error().'];', $headers);
            } else {
                $logmsg .= $today.$title.'SUCCES - nothing to do;'.$endline;
            }
        } else {
            $logmsg .= $today.$title.'SUCCES - '.$rows.' non existant SEF urls were delete;'.$endline;
        }

        // write state to log
        $helper->writeToFile($logmsg);

        // write SQL error if present
        if ($mysqlerrlog != '') {
            $helper->writeToFile($mysqlerrlog, 'a+', 'mysql_errors.log');
        }
?>